<?php
Barakus::import("Framework.Libs.DomitXML.xml_domit_lite_parser");  

class XMLConfigurationHandler extends ConfigurationHandler
{
    
    function setConfiguration()
    {
        $this->configuration = DefaultConfiguration::getDefaultConfiguration(); 
        $this->parseXMLConfigurationFile($this->getConfigurationXMLFile());   
        $this->setUpdateTime(filemtime($this->getConfigurationXMLFile()));
    }    

    function parseXMLConfigurationFile($XMLConfigurationFile)
    {
        $xmldoc         = new DOMIT_Lite_Document($XMLConfigurationFile);
        $success        = $this->xmldoc->loadXML($XMLConfigurationFile, true);
        $arreglo        = $this->xmldoc->toArray();
        
        $configuration = $arreglo['#document'][0]['configuration'];

        foreach($configuration as $elem)
        {
            foreach($elem as $key => $valor)
            {
                $func = str_replace(" ", "", ucwords(str_replace("-", " ", $key)));
                if (!method_exists($this, $func))
                {
                    die("TODO: Error method dont exists " . $func);
                }
                $this->$func($elem[$key], $elem[$key]['attributes']);
            }
        }
    }       
    
    /****************** METODOS DEL CONFIG *******************************/
    /*TODO***************** REFACTORIZAR *********************************/
    
    function ViewsDir($elementos, $atributos)
    {
        $this->configuration['views_dir']      = Barakus::getPath($atributos['url']);
        $this->configuration['views_dir_real'] = $atributos['url'];
    }
    function ViewsCompileDir($elementos, $atributos)
    {
        $this->configuration['views_compile_dir'] = Barakus::getPath($atributos['url']);
        $this->configuration['views_compile_dir_real'] = $atributos['url'];
    }
    function ViewsConfigDir($elementos, $atributos)
    {
        $this->configuration['views_config_dir'] = Barakus::getPath($atributos['url']);
        $this->configuration['views_config_dir_real'] = $atributos['url'];
    }
    function ControllerDir($elementos, $atributos)
    {
        $this->configuration['controller_dir'] = Barakus::getPath($atributos['url']);
        $this->configuration['controller_dir_real'] = $atributos['url'];
    }
    function ModelDir($elementos, $atributos)
    {
        $this->configuration['model_dir'] = Barakus::getPath($atributos['url']);
        $this->configuration['model_dir_real'] = $atributos['url'];
    }
    function Cache($elementos, $atributos)
    {
        $this->configuration['cache_dir'] = Barakus::getPath($atributos['url']);
        $this->configuration['cache_dir_real'] = $atributos['url'];
        $this->configuration['cache']     = $atributos['enabled'];
    }
    function Application($elementos, $atributos)
    {
        $this->configuration['application_mode'] = $atributos['mode'];
        $this->configuration['log_errors']       = empty($atributos['log-errors']) ? 'false' : $atributos['log-errors'];
        $this->configuration['log_type']         = empty($atributos['log-type']) ?   'file'  : $atributos['log-type'];

        $app = array();
        foreach($elementos as $elemento)
        {
            $app = array_merge($app, $elemento);
        }

        $this->configuration['DefaultPage']   = empty($app['default-page']['attributes']['page'])   ? 'Home' : $app['default-page']['attributes']['page'];
        $this->configuration['DefaultAction'] = empty($app['default-page']['attributes']['action']) ? '' :     $app['default-page']['attributes']['action'];

        $this->configuration['debug_error_page']  = $this->__setErrorPage('debug-error-page', $app);
        $this->configuration['online_error_page'] = $this->__setErrorPage('online-error-page', $app);
    }
}
?>
